#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "logger.h"
#include "controlview.h"

using namespace views;
using namespace utilities;

/**
 * @brief MainWindow::MainWindow
 * @param parent
 */
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    _ui(new Ui::MainWindow)
{
    initUI();
    setupUI();
    setupListeners();
}

/**
 * @brief MainWindow::~MainWindow
 */
MainWindow::~MainWindow()
{
    delete _ui;
    delete _headerHBox;
    delete _viewVBox;
    delete _controlView;
    delete _centralWidget;
    delete _viewVLayout;
    delete _configurationView;
    delete _informationView;
    delete _recognitionView;
    delete _mainController;
    delete _actionShowConfig;
    delete _actionShowInfo;
    delete _actionShowRecog;
    delete _actionShowControls;
}

/**
 * @brief MainWindow::initUI
 */
void MainWindow::initUI()
{
    Logger::logMessage("MainWindow::initUI()");

    // Initialize UI objects
    _headerHBox     = new QFrame;
    _viewVBox       = new QFrame;
    _controlView    = new ControlView();
    _centralWidget  = new QWidget(this);
    _viewVLayout    = new QVBoxLayout;
}

/**
 * @brief MainWindow::setupUI
 */
void MainWindow::setupUI()
{
    Logger::logMessage("MainWindow::setupUI()");
    //_ui->setupUi();

    _centralWidget->setMinimumWidth(800);
    _centralWidget->setMinimumHeight(600);
    //_centralWidget->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 255, 255);"));
    //_centralWidget->setStyleSheet("background-image: url(../Recyk/Data/img/texture.png); background-repeat: repeat-xy;");

    // Setup the Header Horizontal Box
    _headerHBox->setStyleSheet("background-image: url(../Recyk/Data/img/texture.png); background-repeat: repeat-xy;");
    _headerHBox->setMaximumHeight(100);
    QHBoxLayout *headerHLayout = new QHBoxLayout;

    // Add logos
    QLabel *logoApp = new QLabel;
    logoApp->setPixmap(QPixmap("../Recyk/Data/img/logoApp.png"));
    QLabel *logoETS = new QLabel;
    logoETS->setPixmap(QPixmap("../Recyk/Data/img/logoETS.png"));

    // Add title
    QLabel *title = new QLabel(QString::fromUtf8("Système d'aide et d'information au recyclage d'objets au niveau des bornes de recyclage"));
    title->setStyleSheet(QString::fromUtf8("font: 18pt \"Rockwell\"; color: rgb(150, 150, 150);"));

    headerHLayout->addWidget(logoApp, 0, Qt::AlignLeft);
    headerHLayout->addWidget(title, 0, Qt::AlignHCenter/* | Qt::AlignBottom*/);
    headerHLayout->addWidget(logoETS, 0, Qt::AlignRight);
    _headerHBox->setLayout(headerHLayout);

    // Setup the View Vertical Box
    //_viewVBox->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 255, 255);"));
    _viewVBox->setLayout(_viewVLayout);
    _viewVBox->setFrameStyle(QFrame::Box);
    QPalette* palette = new QPalette();
    palette->setColor(QPalette::Foreground,Qt::black);
    palette->setColor(QPalette::Background,Qt::white);
    _viewVBox->setPalette(*palette);

    // Setup the main layout
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(_headerHBox);
    mainLayout->addWidget(_viewVBox);
    mainLayout->addWidget(_controlView);

    // Setup the central widget's layout
    _centralWidget->setLayout(mainLayout);
    this->setCentralWidget(_centralWidget);
}

/**
 * @brief MainWindow::setView
 * @param v
 * @param type
 */
void MainWindow::setView(AbstractView *v, ViewTypes type)
{
    Logger::logMessage("MainWindow::setView(AbstractView *v, ViewTypes type)");

    if (type == CONFIGURATION_VIEW)
    {
        _configurationView = v;
        Logger::logMessage("CONFIGURATION_VIEW");
    }
    else if (type == INFORMATION_VIEW)
    {
        _informationView = v;
        Logger::logMessage("INFORMATION_VIEW");
    }
    else if (type == RECOGNITION_VIEW)
    {
        _recognitionView = v;
        Logger::logMessage("RECOGNITION_VIEW");
    }
    else if (type == CONTROL_VIEW)
    {
        _controlView = v;
        Logger::logMessage("CONTROL_VIEW");
    }
}

AbstractView* MainWindow::getView(ViewTypes type)
{
    //Logger::logMessage("MainWindow::getView(ViewTypes type)");

    if (type == CONFIGURATION_VIEW)
    {
        return _configurationView;
    }
    else if (type == INFORMATION_VIEW)
    {
        return _informationView;
    }
    else if (type == RECOGNITION_VIEW)
    {
        return _recognitionView;
    }
    else if (type == CONTROL_VIEW)
    {
        return _controlView;
    }
    else
        return NULL;
}

/**
 * @brief MainWindow::showView
 * @param type
 */
void MainWindow::showView(ViewTypes type)
{
    Logger::logMessage("MainWindow::showView(ViewTypes type)");

    if (type == CONFIGURATION_VIEW)
    {
        if (_configurationView != NULL)
        {
            _configurationView->show();
            _informationView->hide();
            _recognitionView->hide();

            Logger::logMessage("CONFIGURATION_VIEW in display.");
        }
        else
            Logger::logMessage("CONFIGURATION_VIEW is NULL.");
    }
    else if (type == INFORMATION_VIEW)
    {
        if (_informationView != NULL)
        {
            _configurationView->hide();
            _informationView->show();
            _recognitionView->hide();

            Logger::logMessage("INFORMATION_VIEW in display.");
        }
        else
            Logger::logMessage("INFORMATION_VIEW is NULL.");
    }
    else if (type == RECOGNITION_VIEW)
    {
        if (_recognitionView != NULL)
        {
            _configurationView->hide();
            _informationView->hide();
            _recognitionView->show();

            Logger::logMessage("RECOGNITION_VIEW in display.");
        }
        else
            Logger::logMessage("RECOGNITION_VIEW is NULL.");
    }
    else if (type == CONTROL_VIEW)
    {
        if (!_controlView->isVisible())
        {
            this->_controlView->show();
            Logger::logMessage("CONTROL_VIEW in display.");
        }
        else
        {
            Logger::logMessage("CONTROL_VIEW hidden.");
            _controlView->hide();
        }
    }
}

void MainWindow::showViews()
{
    _viewVLayout->addWidget(_configurationView);
    _viewVLayout->addWidget(_informationView);
    _viewVLayout->addWidget(_recognitionView);
}

void MainWindow::setupListeners()
{
    // Initialize Action Event generators
    _actionShowConfig   = new QAction(this);
    _actionShowInfo     = new QAction(this);
    _actionShowRecog    = new QAction(this);
    _actionShowControls = new QAction(this);

    // Setup shortcuts
    _actionShowConfig->setShortcut(tr("Ctrl+C"));
    _actionShowInfo->setShortcut(tr("Ctrl+I"));
    _actionShowRecog->setShortcut(tr("Ctrl+R"));
    _actionShowControls->setShortcut(tr("Ctrl+T"));

    // Setup event listeners
    connect(_actionShowConfig, SIGNAL(triggered()), this, SLOT(showConfigurationView()));
    connect(_actionShowInfo, SIGNAL(triggered()), this, SLOT(showInformationView()));
    connect(_actionShowRecog, SIGNAL(triggered()), this, SLOT(showRecognitionView()));
    connect(_actionShowControls, SIGNAL(triggered()), this, SLOT(showControlsView()));

    // Add actions to the main window
    this->addAction(_actionShowConfig);
    this->addAction(_actionShowInfo);
    this->addAction(_actionShowRecog);
    this->addAction(_actionShowControls);
}

void MainWindow::setMainController(MainController* mc)
{
    _mainController = mc;
}

void MainWindow::showConfigurationView()
{
    Logger::logMessage("SLOT MainWindow::showConfigurationView()");
    showView(CONFIGURATION_VIEW);
}

void MainWindow::showInformationView()
{
    Logger::logMessage("SLOT MainWindow::showInformationView()");
    showView(INFORMATION_VIEW);
}

void MainWindow::showRecognitionView()
{
    Logger::logMessage("SLOT MainWindow::showRecognitionView()");
    showView(RECOGNITION_VIEW);
}

void MainWindow::showControlsView()
{
    Logger::logMessage("SLOT MainWindow::showControlsView()");
    showView(CONTROL_VIEW);

    /*if (!_controlView->isVisible())
    {
        Logger::logMessage("isVisible: false, showing");
        this->_controlView->show();
    }
    else
    {
        Logger::logMessage("isVisible: true, hiding");
        _controlView->hide();
    }*/
}

void MainWindow::startVision()
{
    Logger::logMessage("SLOT MainWindow::startVision()");
    showView(RECOGNITION_VIEW);
    _mainController->startCameraThread();
}

void MainWindow::stopVision()
{
    Logger::logMessage("SLOT MainWindow::stopVision()");
    _mainController->stopCameraThread();
}
